﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace MvePublicService.ClipOperations
{
	/*
	 * 日本SGI株式会社
	 * ヨハンソン・ヘンリク
	 * johansson@sgi.co.jp
	 * 
	 * Change Log:
	 * ---------------------------------------------------------------------------
	 * 2009.01.07	Johansson Henrik		Class Created.
	 * 
	 */

	/// <summary>
	/// Helper class to create Objects from Segment entries in the Database.
	/// Use this class to Load Segments from the Databse.
	/// </summary>
	/// <example>Segment seg = LoadSegmentFromDatabase(1, sqlConnection);</example>
	/// <seealso cref="Segment"/>
	/// <remarks>INSERT:Creds</remarks>
	internal static class SegmentLoader
	{
		/// <summary>
		/// Loads a single Segment from the DB into a Segment object.
		/// Requires that no other commands are open on the connection used to fetch the Segment.
		/// </summary>
		/// <param name="segmentId">Id of segment to load</param>
		/// <param name="connection">Sql Connection</param>
		/// <returns>Object Representation of the loaded Segment.</returns>
		internal static Segment LoadSegmentFromDatabase(int segmentId, SqlConnection connection)
		{
			SqlCommand command = new SqlCommand();
			SqlDataReader reader = null;
			command.CommandTimeout = 60;

			Segment result = null;

			try
			{
				command.Connection = connection;
				command.CommandType = System.Data.CommandType.StoredProcedure;
				command.CommandText = "GetSegmentById";

				command.Parameters.Add("segmentId", SqlDbType.Int).Value = segmentId;
				reader = command.ExecuteReader();

				if (reader.HasRows)
				{
					reader.Read();
					result = new Segment()
					{
						Id = reader.GetInt32(0),
						ParentClipId = reader.GetInt32(1),
						Title = reader.GetString(2),
						Description = reader.GetString(3),
						StartSecond = reader.GetInt32(4),
						EndSecond = reader.GetInt32(5),
						AccessNum = -1,
						ThumbUrl = reader.GetString(7),
						OnAirDate = reader.GetDateTime(8)
					};
				}
			}
			catch (NullReferenceException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			catch (SqlException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			catch (InvalidOperationException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			catch (IndexOutOfRangeException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			finally
			{
				command.Dispose();
				if (reader != null)
				{
					reader.Close();
					reader.Dispose();
				}
			}

			return result;
		}//end:LoadSegmentFromDatabase

		/// <summary>
		/// Loads all Segments belongin to a certain Clip from the DB into a Segment List object.
		/// Requires that no other commands are open on the connection used to fetch the Segment.
		/// </summary>
		/// <param name="clipId">Id of Clip whose Segments to load</param>
		/// <param name="connection">Sql Connection</param>
		/// <returns>List Object Representation of the loaded Segment.</returns>
		internal static List<Segment> LoadSegmentsForClipFromDatabase(int clipId, SqlConnection connection)
		{
			SqlCommand command = new SqlCommand();
			SqlDataReader reader = null;
			command.CommandTimeout = 60;

			List<Segment> result = null;

			try
			{
				command.Connection = connection;
				command.CommandType = System.Data.CommandType.StoredProcedure;
				command.CommandText = "GetSegmentsByClipId";
				command.Parameters.Add("clipId", SqlDbType.Int).Value = clipId;
				reader = command.ExecuteReader();

				if (reader.HasRows)
				{
					result = new List<Segment>();

					while (reader.Read())
					{
						result.Add(new Segment()
						{
							Id = reader.GetInt32(0),
							ParentClipId = reader.GetInt32(1),
							Title = reader.GetString(2),
							Description = reader.GetString(3),
							StartSecond = reader.GetInt32(4),
							EndSecond = reader.GetInt32(5),
							AccessNum = -1,
							ThumbUrl = reader.GetString(7),
							OnAirDate = reader.GetDateTime(8)
						});
					}
				}
			}
			catch (NullReferenceException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			catch (SqlException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			catch (InvalidOperationException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			catch (IndexOutOfRangeException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = null;
			}
			finally
			{
				command.Dispose();
				if (reader != null)
				{
					reader.Close();
					reader.Dispose();
				}
			}

			return result;
		}//end:LoadSegmentsForClipFromDatabase
	}
}
